# 数据写入
# 获取数据
import csv

import requests


def get_data():
    url = "https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1699679803635&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=python&pageIndex=2&pageSize=10&language=zh-cn&area=cn"
    resp = requests.get(url)
    tx_list = resp.json()
    datas_list = list()
    # 处理数据
    for data in tx_list["Data"]["Posts"]:
        RecruitPostName = data.get("RecruitPostName")
        LocationName = data.get("LocationName")
        Responsibility = data.get("Responsibility")
        datas_list.append([RecruitPostName, LocationName, Responsibility])
        print([RecruitPostName, LocationName, Responsibility])
        # break
    return datas_list


# 写入数据
def writer_file():
    datas_list = get_data()
    with open('./tx.csv', mode='w', encoding='utf-8-sig', newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["职位", "地址", "简介"])
        writer.writerows(datas_list)
        print("写入完毕")
    # with open('./tx.csv', mode='w', encoding='gzip', newline="") as f:
    #     writer = csv.writer(f)
    #     writer.writerows(datas_list)
    #     print("写入完毕")


if __name__ == '__main__':
    # get_data()
    writer_file()
